#include "SDialog.h"

#include <core/StyleMgr.h>
#include <widgets/WidgetResizer.h>

#include <QColor>
#include <QHeaderView>
#include <QLabel>
#include <QTableView>
#include <QVBoxLayout>

namespace {
const int margin = 4;
}

SDialog::SDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f) {
  m_clrBack = StyleMgr::instance().color("BasicPlate");

  QVBoxLayout* layout = new QVBoxLayout(this);
  layout->setContentsMargins(0, 0, 0, 0);
  layout->setSpacing(0);
  m_title = new SDialogTitle(this);
  layout->addWidget(m_title);
  setLayout(layout);

  connect(m_title, &SDialogTitle::close, this, &SDialog::reject);
  connect(m_title, &SDialogTitle::popup, this, &SDialog::onPopup);

  m_resizer = new WidgetResizer(this);

  setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
}

void SDialog::setTitle(QString strTitle) {
  m_strTitle = strTitle;
  if (m_title) m_title->setTitle(strTitle);
}

void SDialog::setContent(QWidget* content) {
  QVBoxLayout* mainLayout = qobject_cast<QVBoxLayout*>(layout());
  if (mainLayout) {
    mainLayout->addWidget(content);
    mainLayout->setStretch(0, 0);
    mainLayout->setStretch(1, 1);
  }
}

void SDialog::setEnablePopup(bool enabled) {
  if (m_title) m_title->setEnablePopup(false);
}

void SDialog::setEnableSetting(bool enabled) {
  if (m_title) m_title->setEnableSetting(enabled);
}

void SDialog::addBorder(bool isAddBorder) { m_addBorder = isAddBorder; }

void SDialog::setEnableDisplay(bool enabled) {
  if (m_title) m_title->setEnableDisplay(enabled);
}

void SDialog::showEvent(QShowEvent* event) {
  if (!m_bInited) {
    QString styleSheet;
    if (this->windowFlags() & Qt::Window) {
      m_resizer->enable(true);
      layout()->setContentsMargins(margin, margin, margin, margin);
      m_title->setPopupWindow();

      styleSheet = QString(
                       "SDialog {background-color:#%1;border:1px solid "
                       "#12696B;border-radius:2px}")
                       .arg(m_clrBack.rgb(), 0, 16);
    } else {
      m_resizer->enable(false);
      if (m_addBorder) {
        styleSheet =
            QString("SDialog {background-color:#%1;border:1px solid #%2}")
                .arg(m_clrBack.rgb(), 0, 16)
                .arg(StyleMgr::instance().color("BorderNormal").rgb(), 0, 16);
      } else {
        styleSheet = QString("SDialog {background-color:#%1}")
                         .arg(m_clrBack.rgb(), 0, 16);
        m_title->setChildWindow();
      }
    }
    setStyleSheet(styleSheet);
    m_bInited = true;
  }

  QDialog::showEvent(event);
}

void SDialog::reject() {
  if (0 != (windowFlags() & Qt::Window)) {
    QDialog::reject();
  }
}

void SDialog::onPopup() {
  if (!popup) {
    popup = clone(nullptr);
    popup->setWindowFlag(Qt::Window);
    popup->show();
    int w = width() + margin * 2, h = height() + margin * 2;
    QPoint pt = popup->pos();
    int x = pt.x() + (popup->width() - w) / 2;
    int y = pt.y() + (popup->height() - h) / 2;
    popup->move(x, y);
    popup->resize(w, h);
    popup->setAttribute(Qt::WA_DeleteOnClose);
  } else {
    popup->raise();
  }
}
